package com.oriental.manage.service.specially.Impl;

import com.oriental.check.commons.util.BeanMapperUtil;
import com.oriental.manage.core.paging.Pagination;
import com.oriental.manage.core.response.ResponseDTO;
import com.oriental.manage.core.utils.SessionUtils;
import com.oriental.manage.core.utils.StringUtils;
import com.oriental.manage.dao.specially.BatchMessageMapper;
import com.oriental.manage.pojo.specially.BatchMessage;
import com.oriental.manage.service.specially.BatchMessageProcess;
import com.oriental.manage.service.specially.BatchMessageService;
import com.oriental.opcif.common.util.AssertResultUtils;
import com.oriental.riskfront.api.enums.ErrorCode;
import com.oriental.riskfront.api.enums.MessageStatus;
import com.oriental.riskfront.api.enums.ProcessCode;
import com.oriental.riskfront.api.model.specially.BatchMessageDto;
import com.oriental.riskfront.api.model.specially.ResponseModel;
import com.oriental.riskfront.api.service.BatchMessageInterface;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Author: yutao
 * Date: 2017/4/20
 * Time: 17:11
 * Desc：
 */
@Service
@Slf4j
public class BatchMessageServiceImpl implements BatchMessageService {
    @Autowired
    private BatchMessageMapper batchMessageMapper;

    @Autowired
    private BatchMessageHelper batchMessageHelper;

    @Autowired
    private BatchMessageInterface batchMessageInterface;

    @Override
    public void insertBatchMessage(BatchMessage batchMessage) {
        AssertResultUtils.checkUpdZero(batchMessageMapper.insertModel(batchMessage));

    }

    @Override
    public List<BatchMessage> searchBatchMessage(BatchMessage batchMessage) {
        return batchMessageMapper.queryBathcMessage(batchMessage);
    }

    @Override
    public void queryDetail(BatchMessage batchMessage, ResponseDTO<Pagination> responseDTO) {
        BatchMessageProcess batchMessageProcess = batchMessageHelper.getBatchMessageProcess(batchMessage.getTrnxCode());
        Pagination pagination = new Pagination();
        List list = new ArrayList();
        list.add(batchMessageProcess.queryDetail(batchMessage));
        pagination.setList(list);
        responseDTO.setObject(pagination);
        responseDTO.setSuccess(true);

    }

    @Override
    public void audit(List<BatchMessage> list, ResponseDTO<String> responseDTO) {
        boolean flag = true;
            String userName = SessionUtils.getUserName();
            for(BatchMessage batchMessage:list){
                if(batchMessage.getCreateBy().equals(userName)){
                    flag = false;
                    break;
                }else{
                    batchMessage.setAuditor(userName);
                }
            }
            if(flag) {
            BatchMessage batchMessage = list.get(0);
            BatchMessageProcess batchMessageProcess = batchMessageHelper.getBatchMessageProcess(batchMessage.getTrnxCode());
                log.info("特约商户审核请求信息:{}", list);
            if (batchMessage.getStatus().equals(MessageStatus.REJECT.getCode())) {
                log.info("*********审核拒绝*********");
                dataOperate(list);
                batchMessageProcess.auditFail(list);
                responseDTO.setSuccess(true);
            } else {
                log.info("*********审核成功*********");
                List data = batchMessageProcess.query(list);
                BatchMessageDto batchMessageDto = BeanMapperUtil.objConvert(batchMessage, BatchMessageDto.class);
                ResponseModel<String> responseModel = batchMessageInterface.auditMessage(batchMessageDto, data);
                log.info("特约商户审核响应信息:{}", responseModel);
                if (responseModel.isSuccess()) {
                    String xml = responseModel.getResult();
                    String status = StringUtils.getSubMessage(xml, "<ResultStatus>", "</ResultStatus>");
                    String resultCode = StringUtils.getSubMessage(xml, "<ResultCode>", "</ResultCode>");
                    updataStatus(status,userName,list);
                    batchMessageProcess.callback(status, list);
                    responseDTO.setSuccess(true);
                    responseDTO.setCode(status);
                    responseDTO.setMsg(ProcessCode.getCode(resultCode));

                } else {
                    updataStatus(responseModel.getErrorCode(),userName,list);
                    batchMessageProcess.callback(responseModel.getErrorCode(), list);
                    responseDTO.setSuccess(false);
                    responseDTO.setCode(responseModel.getErrorCode());
                    responseDTO.setMsg(responseModel.getErrorMsg());
                }

            }
        }else {
            responseDTO.setSuccess(false);
            responseDTO.setMsg("报文上报人与审核人不能相同");
        }


        }

    @Transactional
    private void dataOperate(List<BatchMessage> list){
        for(BatchMessage model : list){
            AssertResultUtils.checkUpdZero(batchMessageMapper.updateStatus(model));
        }

    }

    @Transactional
    private void updataStatus(String status,String auditor,List<BatchMessage> list){
        AssertResultUtils.checkUpdZero(batchMessageMapper.updateStatusByList(status,auditor,list));
    }


    }




